<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@page import="net.groupme.gui.constants.VIEW_VOCABULARY"%>
<%@page import="net.groupme.gui.rendering.constants.RENDERING_CONSTANTS"%>
<%@page import="java.util.*"%>
<%@page import="net.groupme.model.*" %>
<%@page import="net.groupme.model.User"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<!-- meta tags, link to css:  -->
	<%@ include file="./jsp-includes/mymetaheader.jsp" %>
	
	<!--  Ajax-Includes -->
    <script type='text/javascript' src='<%= request.getContextPath()%>/dwr/interface/DWRUserManager.js'></script>
    <script type='text/javascript' src='<%= request.getContextPath()%>/dwr/engine.js'></script>
    <script type='text/javascript' src='<%= request.getContextPath()%>/dwr/util.js'></script>
    	<script src="<%= request.getContextPath()%>/scriptaculous/prototype.js" type="text/javascript"></script>
	<script src="<%= request.getContextPath()%>/scriptaculous/scriptaculous.js" type="text/javascript"></script>

<% 
				String username = request.getAttribute(VIEW_VOCABULARY.PARAMETER_USERNAME) + "";
				User user = ObjectFactory.getUser(username);
				List<Group> groupsOfUser = (List<Group>) user.getGroups(MODEL_CONSTANTS.ROLE_OWNER);
				Collections.reverse(groupsOfUser);
				List<Group> subscriptionsOfUser = (List<Group>) user.getGroups(MODEL_CONSTANTS.ROLE_SUBSCRIBER);
		%>
		
	<title><%= user.getFirstName() + " " + user.getLastName() %> [GroupMe!]</title>
	<script type="text/javascript" language="javascript"><!--
	//<![CDATA[//>

	/**
      * Recomputes the User's Tag Cloud.
      */
     function computeTagCloud(){
     
     	$("tagCloudLoadingMessage").show();
     	DWRUserManager.getNonPersonalTagCloudForUsername('<%= username %>', showTagCloud);
     }
   		
	/**
	 * Visualize a tag cloud.
	 */
	 function showTagCloud(tagCloud){
	 	$("tagCloudLoadingMessage").hide();
	 	if(tagCloud == null || tagCloud.html == ""){
	 		$('userTags').innerHTML = "<i>The user hasn't used any tags!</i>";
	 	}else{
	 		$('userTags').innerHTML =  tagCloud.html;
	 		
	 	}
	 }
	
	//init stuff:		
	function init() {
		DWRUtil.useLoadingMessage();
		computeTagCloud();  
	}
	//<!]]>
	//-->
	</script>
</head>
<body onload="init();">
	<!-- title image, logo:  -->
	<div id="groupme_header">
		<%@ include file="./jsp-includes/groupme_header.jsp" %>
	</div>
	
	<!-- Menu: -->
	<div id="menu">
		<%@ include file="./jsp-includes/groupme_menu.jsp" %>
	</div>
	<!-- content and sidebar -->
	<!-- Content: -->
	<div id="mycontent">
		<div style="margin-bottom: 10px;">
			<div class="ro_w">
				<div class="lo_w">
					<div class="ru_w">
						<div class="lu_w">
							<div style="height:30px; line-height:30px; padding-left:1em;">
							<h3><%= username %>'s profile</h3>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	
	
		<div id="main_home">
			<div class="ro_bw">
				<div class="lo_bw">
					<div class="ru_bw">
						<div class="lu_bw">
							<div id="user_groups">
								<div class="content_header" id="grouplist_header" style="padding-top: 2px">
									<small style="color: #006699;">
									<span id="showsubsorgroups">
									
									<%
									if(request.getParameter("show") == null){
										%>
										[<a><%= username %>'s groups</a>]
										<%
										if(subscriptionsOfUser.size() > 0){ %>
										[<a href="<%=request.getContextPath() + "/user/" + username + "?show=subscriptions" %>"><%= username %>'s favorites</a>]
										<%}  %>
										<%
									}else {
										%>
										[<a href="<%=request.getContextPath() + "/user/" + username%>"><%= username %>'s groups</a>]
										[<a><%= username %>'s favorites</a>]
										<%
									}
									
									%>
									</span> 
									</small>
								</div>
								<div class="generalContent">
								<%
								
								int currentPage = 0;
								if(request.getParameter("pageID") != null){
									currentPage = Integer.parseInt(request.getParameter("pageID"));
								}else {
									currentPage = 1;
								}
								
								//start and end of the list-iterator
								int iterStart = (currentPage * 10) - 10;
								int iterEnd = iterStart + 9;
								int numberofGroups = 0;
								//List to be displayed (groups or subscriptions)
								List<Group> displayedList = null;
								String showParameter = null;
								if(request.getParameter("show") == null){
									//display groups of user
									displayedList = groupsOfUser;
									numberofGroups = groupsOfUser.size();
									%>
									<div id="groups">
									<h4><%= username %>'s groups:</h4>
									<%
										ListIterator<Group> it = groupsOfUser.listIterator(iterStart);
										boolean editGroups = false;
									%>
									<%@ include file="./jsp-includes/groupme_list_groups.jsp" %>
									</div>
									
								<%	
								}else{
									//display user favorites
									displayedList = subscriptionsOfUser;
									numberofGroups = subscriptionsOfUser.size();
									showParameter = "show=subscriptions";
								%>
									
									<div id="subscriptions">
									<h4><%= username %>'s favorites:</h4>
									<% 
										ListIterator<Group> it = subscriptionsOfUser.listIterator(iterStart);
										boolean editGroups = false;
									%>
									
									<%@ include file="./jsp-includes/groupme_list_groups.jsp" %>
									
									</div>
									<%
								}
								%>
	<div id="pagenumbers" style="text-align: center; margin-top: 1em; font-size:0.8em;">
	<%
		int resultListSize = numberofGroups;
		if(resultListSize > 10){
			int numberofResultpages = (int) Math.ceil(resultListSize / 10.0);
			String pagerequestURL = request.getContextPath() + "/user/" + username;
			
			if(showParameter != null){
				pagerequestURL = pagerequestURL + "?" + showParameter + "&pageID=";
			}else {
				pagerequestURL = pagerequestURL + "?pageID=";
			}
			if(currentPage > 1){
				int showPrev = currentPage-1;
				%>
				<span><strong><a href="<%= pagerequestURL + showPrev %>">previous</a></strong></span>&nbsp;&nbsp;
				<%
			}
			out.print("<span>");
			if(numberofResultpages <= 10){
				for(int i = 1; i <= numberofResultpages; i++){
					%>
					<a href="<%= pagerequestURL + i %>" <%if(i == currentPage){out.print("style=\"font-weight:bold;\"");}%>><%=i %></a>
					<%
				}
			}else {
				if(numberofResultpages - currentPage >= 10){
					int start = 0;
					int end = 0;
					
					if(currentPage < 7){
						start = 1;
						end = 7;
					}else {
						
						%>
						<a href="<%= pagerequestURL + 1 %>"><%=1 %></a>
						<%
						out.print("&nbsp;...&nbsp;");
						start = currentPage - 3;
						end = currentPage + 3;
					}
					
					for(int i = start; i <= end; i++){
						%>
						<a href="<%= pagerequestURL + i %>" <%if(i == currentPage){out.print("style=\"font-weight:bold;\"");}%>><%=i %></a>
						<%
					}
					out.print("&nbsp;...&nbsp;");
					%>
					<a href="<%= pagerequestURL + numberofResultpages %>"><%=numberofResultpages %></a>
					<%
				}
				else {
					%>
					<a href="<%= pagerequestURL + 1 %>"><%=1 %></a>
					<%
					out.print("&nbsp;...&nbsp;");
					//out.print("...&nbsp;");
					for(int i = numberofResultpages - 10; i <= numberofResultpages; i++){
						%>
						<a href="<%= pagerequestURL + i %>" <%if(i == currentPage){out.print("style=\"font-weight:bold;\"");}%>><%=i %></a>
						<%
					}
				}
			}
			out.print("</span>");
			if(currentPage < numberofResultpages){
				int showNext = currentPage+1;
				%>
				<span><strong><a href="<%= pagerequestURL + showNext %>">next</a></strong></span>&nbsp;&nbsp;
				<%
			}
		}
	%>
</div>		
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		
		</div>
		
		
		<div id="sidebar_right">
			<div id="latestUsers">
				<div class="ro_bw">
					<div class="lo_bw">
						<div class="ru_bw">
							<div class="lu_bw">
								<div id="user_data">
									<div class="content_header" style="color: #006699; font-size: small; padding-top: 2px;">
										Facts about <%= username %>
									</div>
									<div class="generalContent">
										<div style="float: left; width: 75px;">
										<% if(user.getPhoto() != null && !"".equals(user.getPhoto())){ %>
											<img src='<%= user.getPhoto() %>' width="70px" alt='user photo'/>
										<%}else{ %>
											<img src='<%= VIEW_VOCABULARY.STANDARD_USER_ICON %>' width="70px" alt='user photo'/>
										<%} %>
										</div>
										<div>
											<i><%= user.getFirstName() + " " + user.getLastName() %></i>
											<% if(user.getFoaf() != null){ %>
												<a href="http://xml.mfd-consult.dk/foaf/explorer/?foaf=<%=user.getFoaf() %>" title="Explore FOAF description...">
												   <img style="text-decoration:none;border:none;"  src="http://www.foaf-project.org/images/foafTiny.gif"/>
												</a>
											<%} %>
											<br />
											<br />
											
										</div>
										<br style="clear:left;"/>
										<div id="numbers" style="font-size: small;">
											<%= username %> created <a href="#groups"><%= groupsOfUser.size() %> groups</a>, has <a href="#subscriptions"><%= subscriptionsOfUser.size() %> favorite groups</a>, and made <%= user.getTags().size() %> tag assignments!
										</div>
									</div>
									<div>
									</div>
								</div>
							</div>	
						</div>
					</div>
				</div>
			</div>
			<div id="tagCloud">
				<div class="ro_bw">
					<div class="lo_bw">
						<div class="ru_bw">
							<div class="lu_bw">
								<div id="user_tagCloud" style="color: #006699; font-size: small; padding-top: 2px;">
									<div class="content_header">
										<%= username %>'s tags
									</div>
									<div id="userTags" class="generalContent">
									</div>
									<span id="tagCloudLoadingMessage">
										<img src="<%= request.getContextPath() + RENDERING_CONSTANTS.RELATIVE_PATH_OF_AJAX_LOADING_IMAGE %>" alt="loading"/> 
										<small><i>refreshing tag cloud</i></small>
									</span>
								</div>
							</div>	
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	<div id="footer">
		<%@ include file="./jsp-includes/footer.jsp" %>
	</div>
</body>
</html>